package com.wslNB.goods.controller;


import com.wslNB.entity.Result;
import com.wslNB.entity.StatusCode;
import com.wslNB.goods.pojo.Goods;
import com.wslNB.goods.pojo.Spu;
import com.wslNB.goods.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/spu")
@CrossOrigin
public class SpuController {


    @Autowired
    private SpuService spuService;

    /**
     * 添加spu
     * @param goods
     * @return
     */
    @PostMapping("/addSpu")
    public Result addSpu(@RequestBody Goods goods){

        try {
            spuService.addSpu(goods);
            return new Result(true,StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }

    }


    /**
     *根据ID查询goods--spu and sku
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable String id){

        try {
            Goods goods = spuService.findById(id);
            return new Result(true,StatusCode.OK,"查询成功",goods);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }


    /**
     * 修改商品信息
     * @param goods
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Goods goods){

        try {
            spuService.update(goods);
            return new Result(true,StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
    }


    /**
     * 商品审核1
     * @param id
     * @return
     */
    @PutMapping("/audit/{id}")
    public Result audit(@PathVariable String id){

        try {
            spuService.audit(id);
            return new Result(true,StatusCode.OK,"审核成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"审核失败");
        }
    }


    /**
     * 商品下架
     * @param id
     * @return
     */
    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable String id){

        try {
            spuService.pull(id);
            return new Result(true,StatusCode.OK,"下架成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"下架失败");
        }
    }


    /**
     * 商品上架
     * @param id
     * @return
     */
    @PutMapping("/put/{id}")
    public Result put(@PathVariable String id){

        try {
            spuService.put(id);
            return new Result(true,StatusCode.OK,"上架成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"上架失败");
        }

    }


    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @PutMapping("/delete/{id}")
    public Result delete(@PathVariable String id){
        try {
            spuService.delete(id);
            return new Result(true,StatusCode.OK,"逻辑删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"逻辑删除失败");
        }
    }


    /**
     * 还原被逻辑删除的商品
     * @param id
     * @return
     */
    @PutMapping("/restore/{id}")
    public Result restore(@PathVariable String id){
        try {
            spuService.restore(id);
            return new Result(true,StatusCode.OK,"还原删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"还原删除失败");
        }
    }


    /**
     * 物理删除
     * @param id
     * @return
     */
    @DeleteMapping("/realDelete/{id}")
    public Result realDelete(@PathVariable String id){
        try {
            spuService.realDelete(id);
            return new Result(true,StatusCode.OK,"物理删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"物理删除失败");
        }
    }


}
